﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
	public class UserData_56_IdentificationNumber : UserData
	{
		#region constants

		private const byte MaxDigits = 12;

		#endregion constants

		#region private member variables

		private readonly string _identificationNumber;

		#endregion private member variables

		#region constructor


		public UserData_56_IdentificationNumber(string identificationNumber)
			: base(UserData.UserData56IdentificationNumber)
		{
			// set default value
			_identificationNumber = "0";
			if (String.IsNullOrEmpty(identificationNumber))
			{
				// TODO handle error ...
			}
			else if (identificationNumber.Length > MaxDigits)
			{
				// the id must have only 12 digits
				// TODO handle error ...
			}
			else
			{
				UInt64 uint64Result;
				bool bRetVal = UInt64.TryParse(identificationNumber, out uint64Result);
				if (bRetVal)
				{
					_identificationNumber = identificationNumber;
				}
				else
				{
					// TODO handle error ...
				}
			}
		}

		#endregion constructor

        #region getters

		public string IdentificationNumber { get { return _identificationNumber; } }

        #endregion getters

		#region ToString, Equals, GetHashCode

		public override int GetHashCode()
		{
			int hashCode = base.GetHashCode();

			for (int index = 0; index < _identificationNumber.Length; index++ )
			{
				hashCode ^= (byte)(_identificationNumber[index]);
			}

			return hashCode;
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("(");
			stringBuilder.Append(ToStringProperties());
			stringBuilder.Append(")");

			return stringBuilder.ToString();
		}

		internal override string ToStringProperties()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(base.ToStringProperties());

			stringBuilder.Append(", ");

			stringBuilder.Append("IdentificationNumber='");
			stringBuilder.Append(IdentificationNumber);
			stringBuilder.Append("'");

			return stringBuilder.ToString();
		}

		public override bool Equals(object obj)
		{
			Type objType = obj.GetType();
			if (GetType() == objType)
			{
				UserData_56_IdentificationNumber userData2Compare = obj as UserData_56_IdentificationNumber;
				return EqualsProperties(userData2Compare);
			}

			return false;
		}

		internal bool EqualsProperties(UserData_56_IdentificationNumber userData2Compare)
		{
			if (base.EqualsProperties(userData2Compare))
			{
				return (String.Compare(IdentificationNumber, userData2Compare.IdentificationNumber, true) == 0);
			}

			return false;
		}

		#endregion ToString, Equals, GetHashCode
	}
}
